package edu.vt.ece3574.ninjatag;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import android.util.Log;

import com.facebook.android.FriendsGetProfilePics;
import com.facebook.android.Utility;

public class GamesList {
	
	
	private static HashMap<String,GameIf> gamesList_; //The string will be the Event ID, such as the one Facebook generates.
	private boolean fetching = false;
	
	public LinkedList<String> gameNameList;
	
	public boolean available() {
		return !fetching;
	}
	
	public GamesList() {
		gamesList_=new HashMap<String, GameIf>();
		gameNameList = new LinkedList<String>();
		
	}
	
	public GamesList(boolean a) {
		Log.d("FL","AG");
		fetching = true;
		gamesList_=new HashMap<String, GameIf>();
		gameNameList = new LinkedList<String>();

		if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        //Utility.model.setListener(this);
		
		/*// TODO: figure out how to find events from Facebook. (i.e. what tag/keyword/...to query)
        String query = "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) AND is_app_user = 1 order by name";
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        Utility.mAsyncRunner.request(null, params,
                new FriendsRequestListener());
		Log.d("FL","OW");//*/
        
        if (a) {
        	put("dummy",1,new DummyGame());
        }
        
        
	}
	
	public void put(String type, long id, GameIf game) {
		gamesList_.put(""+type.charAt(0)+id, game);
	}
	
	public void put(String ninjaTagID, GameIf game) {
		gamesList_.put(ninjaTagID, game);
	}
	
	public GameIf get(String type, long id) {
		return gamesList_.get(""+type.charAt(0)+id);
	}
	
	public GameIf get(String ninjaTagID) {
		return gamesList_.get(ninjaTagID);
	}
	
	public boolean contains(String type, long id) {
		return gamesList_.containsKey(""+type.charAt(0)+id);
	}
	
	public boolean contains(String ninjaTagID) {
		return gamesList_.containsKey(ninjaTagID);
	}
	
	public int getCount() {
		return gamesList_.size();
	}
	
	public LinkedList<String> getKeys() {
		return new LinkedList<String>(gamesList_.keySet());
	}

	public LinkedList<GameIf> getAll() {
		return new LinkedList<GameIf>(gamesList_.values());
	}

	public class DummyGame implements GameIf {
	
		@Override
		public String getName() {
			return "Super Special Awesome Sexy Yu-Gi-Oh Abridged Reference Go!"; //TODO: Change this. I'm delusional on copious amounts of stress. ^^;;
		}
		
		@Override
		public List<PlayerIf> getPlayers() {
			return Utility.mFriends.getAll();
		}

		@Override
		public long endTime() {
			return 0; //Zero means unlimited time. I'll document this elsewhere.
		}

		@Override
		public String getType() {
			return "A fun game";
		}
	
	}
	
	public void addName(String name){
		gameNameList.add(name);
	}
	
	public String[] getNameList(){
	
		String[] temp = new String[]{"No Games"};
		if(gameNameList.size() > 0){
			
			LinkedList<String> tempList = new LinkedList<String>();
			tempList = gameNameList;
			for(int i = 0; i < gameNameList.size(); i++){
				
				temp[i] = tempList.removeFirst();
			}
			return temp;
		}
		else return temp;
	
	}
}
